public class MySingleList {
    public MySingleList.ListNode head;

    public MySingleList() {
    }

    public void createList() {
        MySingleList.ListNode listNode1 = new ListNode(12);
        MySingleList.ListNode listNode2 = new ListNode(23);
        MySingleList.ListNode listNode3 = new ListNode(34);
        MySingleList.ListNode listNode4 = new ListNode(45);
        MySingleList.ListNode listNode5 = new ListNode(56);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        this.head = listNode1;
    }

    public void display() {
        for (MySingleList.ListNode cur = this.head; cur != null; cur = cur.next) {
            System.out.print(cur.val + " ");
        }

        System.out.println();
    }

    public int size() {
        int size = 0;

        for (MySingleList.ListNode cur = this.head; cur != null; cur = cur.next) {
            ++size;
        }

        return size;
    }

    public boolean contains(int key) {
        for (MySingleList.ListNode cur = this.head; cur != null; cur = cur.next) {
            if (cur.val == key) {
                return true;
            }
        }

        return false;
    }

    public void addFirst(int data) {
        MySingleList.ListNode node = new ListNode(data);
        node.next = this.head;
        this.head = node;
    }

    public void addLast(int data) {
        MySingleList.ListNode node = new ListNode(data);
        MySingleList.ListNode cur = this.head;
        if (cur == null) {
            this.head = node;
        } else {
            while (true) {
                if (cur.next == null) {
                    cur.next = node;
                    break;
                }

                cur = cur.next;
            }
        }

    }

    public void addIndex(int index, int data) {
        if (index >= 0 && index <= this.size()) {
            if (index == 0) {
                this.addFirst(data);
            } else if (index == this.size()) {
                this.addLast(data);
            } else {
                MySingleList.ListNode cur = this.findIndexSubOne(index);
                MySingleList.ListNode node = new ListNode(data);
                node.next = cur.next;
                cur.next = node;
            }
        } else {
            System.out.println("index位置不合法");
            //throw new IndexWrongfulException("index位置不合法");
        }
    }

    private MySingleList.ListNode findIndexSubOne(int index) {
        MySingleList.ListNode cur;
        for (cur = this.head; index - 1 != 0; --index) {
            cur = cur.next;
        }

        return cur;
    }

    public void remove(int key) {
        if (this.head == null) {
            System.out.println("链表为空");
        } else if (this.head.val == key) {
            this.head = this.head.next;
        } else {
            MySingleList.ListNode cur = this.findPrevOfKey(key);
            if (cur == null) {
                System.out.println("没有你要删除的数据");
            } else {
                cur.next = cur.next.next;
            }

        }
    }

    private ListNode findPrevOfKey(int key) {
        ListNode cur = this.head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    public MySingleList.ListNode removeAllKey(int key) {
        if (this.head == null) {
            return null;
        }
        ListNode cur = this.head.next;
        ListNode prev = this.head;
        while (cur != null) {
            if (cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }
        if (this.head.val == key) {
            this.head = head.next;
        }
        return head;
    }

    static class ListNode {
        int val;
        MySingleList.ListNode next;

        ListNode(int val) {
            this.val = val;
        }
    }

    // 合并两个有序的单链表
    public ListNode Merge(ListNode list1,ListNode list2) {
        if (list1 == null) {
            return null;
        }
        ListNode cur = list1.val > list2.val ? list2 : list1;

    }
}